package com.dlc.shop.delivery.platform.controller;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.vo.OutletConfigInfoVO;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.delivery.common.model.OutletConfig;
import com.dlc.shop.delivery.common.service.OutletConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 网点配置
 * @author TRACK
 */
@RestController
@RequestMapping("/platform/outletConfig")
@Tag(name = "网点配置")
@AllArgsConstructor
public class OutletConfigController {

    private final OutletConfigService outletConfigService;

    @GetMapping("/page")
    @Operation(summary = "获取网点配置列表", description = "分页获取网点配置列表")
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:page')")
    public ServerResponseEntity<IPage<OutletConfig>> getOutletConfigPage(PageParam<OutletConfig> page, OutletConfig outletConfig) {
        PageParam<OutletConfig> outletConfigPage = outletConfigService.page(page, new LambdaQueryWrapper<OutletConfig>()
                .eq(Objects.nonNull(outletConfig.getDeliveryCompanyType()), OutletConfig::getDeliveryCompanyType, outletConfig.getDeliveryCompanyType())
                .eq(OutletConfig::getShopId, Constant.PLATFORM_SHOP_ID));
        for (OutletConfig record : outletConfigPage.getRecords()) {
            if (StrUtil.isNotBlank(record.getMobile())) {
                record.setMobile(PhoneUtil.hideBetween(record.getMobile()).toString());
            }
        }
        return ServerResponseEntity.success(outletConfigPage);
    }

    @GetMapping("/info/{outletConfigId}")
    @Operation(summary = "获取网点配置", description = "根据outletConfigId获取网点配置")
    @Parameter(name = "outletConfigId", description = "网点配置", required = true)
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:get')")
    public ServerResponseEntity<OutletConfig> getById(@PathVariable("outletConfigId") Long outletConfigId) {
        OutletConfig outletConfig = outletConfigService.getById(outletConfigId);
        checkAuth(outletConfig);
        if (StrUtil.isNotBlank(outletConfig.getMobile())) {
            outletConfig.setMobile(PhoneUtil.hideBetween(outletConfig.getMobile()).toString());
        }
        return ServerResponseEntity.success(outletConfig);
    }

    private static void checkAuth(OutletConfig outletConfig) {
        if (Objects.isNull(outletConfig)) {
            throw new YamiShopBindException("yami.outlet.config.is.null");
        }
        if (!Objects.equals(outletConfig.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            throw new YamiShopBindException("yami.operate.auth");
        }
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:save')")
    @Operation(summary = "保存网点配置", description = "保存网点配置")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid OutletConfig outletConfig) {
        outletConfig.setShopId(Constant.PLATFORM_SHOP_ID);
        outletConfig.setOutletConfigId(null);
        outletConfigService.insert(outletConfig);
        outletConfigService.removeCache(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:update')")
    @Operation(summary = "更新网点配置", description = "更新网点配置")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid OutletConfig outletConfig) {
        outletConfig.setShopId(Constant.PLATFORM_SHOP_ID);
        outletConfigService.update(outletConfig);
        outletConfigService.removeCache(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{outletConfigId}")
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:delete')")
    @Operation(summary = "删除网点配置", description = "根据网点配置id删除网点配置")
    @Parameter(name = "outletConfigId", description = "网点配置", required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long outletConfigId) {
        OutletConfig dbOutletConfig = outletConfigService.getById(outletConfigId);
        checkAuth(dbOutletConfig);
        outletConfigService.removeById(outletConfigId);
        outletConfigService.removeCache(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @PutMapping("/setDefault/{outletConfigId}")
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:setDefault')")
    @Operation(summary = "设网点信息为默认/取消默认")
    public ServerResponseEntity<Void> setDefault(@PathVariable Long outletConfigId) {
        OutletConfig dbOutletConfig = outletConfigService.getById(outletConfigId);
        checkAuth(dbOutletConfig);
        outletConfigService.setDefault(outletConfigId);
        outletConfigService.removeCache(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @GetMapping("/listOutletConfig")
    @Operation(summary = "获取网点信息列表")
    @PreAuthorize("@pms.hasPermission('delivery:outletConfig:list')")
    public ServerResponseEntity<List<OutletConfigInfoVO>> listOutletConfig() {
        List<OutletConfigInfoVO> list = outletConfigService.listByShopId(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success(list);
    }
}
